package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.ErrorCode;
import com.example.constant.CommonConstant;
import com.example.exception.BusinessException;
import com.example.exception.ThrowUtils;
import com.example.mapper.AiFrequencyOrderMapper;
import com.example.model.dto.order.AiFrequencyOrderQueryRequest;
import com.example.model.entity.AiFrequencyOrder;
import com.example.model.entity.User;
import com.example.model.enums.PayOrderEnum;
import com.example.utils.SqlUtils;
import com.example.model.dto.order.AiFrequencyOrderUpdateRequest;
import com.example.service.AiFrequencyOrderService;
import com.example.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.*;
import java.util.Date;


@Service
public class AiFrequencyOrderServiceImpl extends ServiceImpl<AiFrequencyOrderMapper, AiFrequencyOrder>
        implements AiFrequencyOrderService {

    @Resource
    private UserService userService;


    /**
     * 分页获取订单列表
     *
     * @param orderQueryRequest
     * @return
     */
    public QueryWrapper<AiFrequencyOrder> getOrderQueryWrapper(AiFrequencyOrderQueryRequest orderQueryRequest) {

        Long id = orderQueryRequest.getId();
        Double price = orderQueryRequest.getPrice();
        Long purchaseQuantity = orderQueryRequest.getPurchaseQuantity();
        Double totalAmount = orderQueryRequest.getTotalAmount();
        Integer orderStatus = orderQueryRequest.getOrderStatus();
        Long userId = orderQueryRequest.getUserId();
        Date createTime = orderQueryRequest.getCreateTime();

        String sortField = orderQueryRequest.getSortField();
        String sortOrder = orderQueryRequest.getSortOrder();

        QueryWrapper<AiFrequencyOrder> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(id != null && id > 0, "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(orderStatus),"orderStatus",orderStatus);
        queryWrapper.eq(ObjectUtils.isNotEmpty(price),"price",price);
        queryWrapper.eq(ObjectUtils.isNotEmpty(purchaseQuantity),"purchaseQuantity",purchaseQuantity);
        queryWrapper.eq(ObjectUtils.isNotEmpty(totalAmount),"totalAmount",totalAmount);

        LocalDateTime startOfDay = null;
        LocalDateTime endOfDay = null;
        if (ObjectUtils.isNotEmpty(createTime)){
            ZonedDateTime zonedDateTime = createTime.toInstant().atZone(ZoneId.systemDefault());
            LocalDate createTimeLocalDate = zonedDateTime.toLocalDate();
            startOfDay = createTimeLocalDate.atStartOfDay();
            endOfDay = createTimeLocalDate.atTime(LocalTime.MAX);
        }
        queryWrapper.between(ObjectUtils.isNotEmpty(startOfDay),"createTime", startOfDay, endOfDay);
        queryWrapper.eq("isDelete", false);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_DESC),
                sortField);
        return queryWrapper;
    }

    /**
     * 修改订单
     *
     * @param orderUpdateRequest
     * @param request
     * @return
     */
    @Override
    public boolean updateOrderInfo(AiFrequencyOrderUpdateRequest orderUpdateRequest, HttpServletRequest request) {
        Long purchaseQuantity = orderUpdateRequest.getPurchaseQuantity();
        Long id = orderUpdateRequest.getId();
        User loginUser = userService.getLoginUser(request);
        if (id < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "订单不存在");
        }

        if (loginUser == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户未登录");
        }
        if (purchaseQuantity < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "输入正确的购买数量");
        }
        AiFrequencyOrder order = new AiFrequencyOrder();
        BeanUtils.copyProperties(orderUpdateRequest, order);
        order.setId(id);
        Double price = 0.1;
        order.setTotalAmount(purchaseQuantity * price);
        order.setOrderStatus(Integer.valueOf(PayOrderEnum.WAIT_PAY.getValue()));
        boolean result = this.updateById(order);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return true;
    }
}




